package WIAB::Controller::Content;

use strict;
use warnings;
use base 'Catalyst::Controller';

use Path::Class;
use XML::Feed;

sub default : Path {
    my ( $self, $c ) = @_;

    $c->stash->{env} = \%ENV;
    $c->stash->{MainTitle} = $c->config->{description};

    # work out what page to retrieve
    my $args = $c->req->args;
    my $path = join '/', @$args;
    my $start = Path::Class::Dir->new($c->config->{content});
    # check to display delete this page links
    if ( scalar(@$args) == 0) {
        $c->stash->{IsTerminalOrRoot} = 1;
    }
    elsif ( ! $c->model('Content')->_is_terminal($args, $start) ) {
        $c->stash->{IsTerminalOrRoot} = 1;
    }

    # retrieve the page and pop everything relevant into the stash
    ($c->stash->{Title}, $c->stash->{Content}, $c->stash->{Error}) =
        $c->model('Content')->
            get_xhtml_content($start->subdir(join '/',  @$args));
    $c->stash->{Title} = $c->config->{name}  . ' | ' . $c->stash->{Title};
    $c->res->status(404) if $c->stash->{Error};

    # menu stuff
    $c->stash->{Breadcrumb} = $c->model('Content')->make_breadcrumbs($args);
    $c->stash->{Menu} = $c->model('Content')->
        make_menu($args);
    $c->stash->{last_modified} = $c->model('Content')->get_last_modified_time($args, $start, $c->user);
    $c->stash->{template} = 'content.tt';
}

sub rss : Local {
    my ($self, $c)= @_;
    my $path = $c->req->args;
    my $base = Path::Class::Dir->new($c->config->{content});
    my $rss_data = $c->model('Content')->get_rss_data($base, $path);

    # this is view code which is in the controller really.  It could
    # be in the model, but then we would have to mess around with
    # ACCEPT_CONTEXT or uglier.
    my $feed = XML::Feed->new('RSS');
    if (@$path) {
        my $subpage = join '/', @$path;
        $feed->title( $c->config->{name} . ' for ' . $subpage  .' page RSS Feed' );
    }
    else {
    $feed->title( $c->config->{name} . ' RSS Feed' );
    }

    $feed->link( $c->uri_for("/content/") );
    $feed->description( $c->config->{site_descr} );
    foreach my $entry ( @$rss_data ) {
        my $feed_entry = XML::Feed::Entry->new('RSS');
        $feed_entry->title( $entry->{title} );
        $feed_entry->link( $c->uri_for("/content/" . $entry->{link}));
        $feed_entry->summary($entry->{summary});
        $feed_entry->issued( DateTime->from_epoch(epoch   => $entry->{issued} ) );
        $feed->add_entry($feed_entry);
    }
    $c->res->content_type('application/rss+xml');
    $c->res->body( $feed->as_xml );
}

=head1 NAME

WIAB::Controller::Content - Catalyst Controller

=head1 SYNOPSIS

See L<WIAB>

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut

=head1 default

This is the only action in this controller class.  It finds the
template root and based on the path args gathers up the index.pod file
in the traling path and renders it as html.  so /content/one/two will
render the file index.pod in the directory root/content/one/two for
example.

It also gathers up the menu and breadcrumbs from the model and provides data structures for rendering too.

=head1 AUTHOR

Kieren Diment

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
